<template>
  <div class="app-container">
    <el-form :inline="true" :model="query" class="demo-form-inline">
      <el-form-item label="文章标题">
        <el-input v-model="query.title" placeholder="审批人"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="query.status" placeholder="活动区域">
          <el-option label="未审核" value="1"></el-option>
          <el-option label="已审核" value="2"></el-option>
          <el-option label="已删除" value="0"></el-option>
          <el-option label="审核未通过" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="queryData">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="reload">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="list" style="width: 100%" border stripe>
      <el-table-column align="center" type="index" label="序号" width="80"></el-table-column>
      <el-table-column align="center" prop="title" label="文章标题"></el-table-column>
      <el-table-column align="center" prop="thumhup" label="浏览量"></el-table-column>
      <el-table-column align="center" prop="ispublic" label="是否公开">
        <!-- 公开和不公开 -->
        <template slot-scope="scope">
          <!-- <el-tag :type="scope.row.ispublic==1?'success':'danger'">{{scope.row.ispublic==1?'公开':'不公开'}}</el-tag> -->
          <!-- 过滤器 -->
          <el-tag :type="scope.row.ispublic | publicFilter">{{scope.row.ispublic==1?'公开':'不公开'}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="status" label="状态">
        <!-- 删除和审核通过 -->
        <template slot-scope="scope">
          <!-- 过滤器 -->
          <el-tag
            :type="scope.row.status | statusFilter"
          >{{scope.row.status | statusFilter | titleFilter}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="updateDate" label="最后更新时间" min-width="90px">
        <template slot-scope="scope">{{getFormat(scope.row.updateDate)}}</template>
      </el-table-column>
      <el-table-column align="left" label="操作" min-width="90px">
        <template slot-scope="scope" style="width:300px">
          <el-button size="mini" @click="open(scope.row.id)">查看</el-button>
          <!-- // 0: 已删除, 1:未审核，2:已审核,3: 审核未通过 -->
          <el-button
            size="mini"
            type="success"
            v-if="scope.row.status==1"
            @click="openAudit(scope.row.id)"
          >审核</el-button>
          <el-button
            size="mini"
            v-if="scope.row.status!=0"
            @click="handleDelete(scope.row.id)"
            type="danger"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
    ></el-pagination>
    <audit
      :id="artivle.id"
      :isAudit="artivle.isAudit"
      :Visible="artivle.dialogVisible"
      :title="artivle.title"
      :remoteClose="remoteClose"
    ></audit>
  </div>
</template>
<script>
import articleApi from "@/api/article";
import { format } from "@/utils/date";
import audit from "./audit";

export default {
  components: { audit },
  name: "Article",
  data() {
    return {
      list: [],
      page: {
        current: 1,
        size: 20,
        total: 0
      },
      query: {},
      artivle: {
        isAudit: true, //审核界面是否
        id: null,
        dialogVisible: false,
        title: ""
      }
    };
  },
  created() {
    this.fetchData();
  },
  filters: {
    publicFilter(value) {
      const state = { 0: "danger", 1: "success" };
      return state[value];
    },
    statusFilter(value) {
      // 0: 已删除, 1:未审核，2:已审核,3: 审核未通过
      const state = { 0: "danger", 1: "warning", 2: "success", 3: "info" };

      return state[value];
    },
    titleFilter(value) {
      const state = {
        danger: "已删除",
        warning: "未审核",
        success: "已审核",
        info: "审核未通过"
      };
      return state[value];
    }
  },
  methods: {
    // 审核
    openAudit(id) {
      this.artivle.dialogVisible = true;
      this.artivle.title = "审核";
      this.artivle.id = id; //文章id
      this.artivle.isAudit = true; //表示为什么界面（true=审核界面） false=查看界面
    },
    // 查看
    open(id) {
      this.artivle.title = "查看";
      this.artivle.id = id;
      this.artivle.dialogVisible = true;
      this.artivle.isAudit = false; //表示为什么界面
    },
    // 关闭窗口.
    remoteClose() {
      this.artivle.dialogVisible = false;
      this.fetchData();
    },
    // 重置
    reload() {
      this.query = {};
      this.fetchData();
    },
    // 条件查询
    queryData() {
      this.page.current = 1;
      this.query = {};
      this.fetchData();
    },
    // 时间格式
    getFormat(time) {
      return format(time);
    },
    handleSizeChange(value) {
      // 当前页数多少条
      this.page.size = value;
      // 重新查询
      this.fetchData();
    },
    handleCurrentChange(value) {
      // 当前多少页数
      this.page.current = value;
      // 重新查询
      this.fetchData();
    },
    async fetchData() {
      const { data } = await articleApi.getList(
        this.query,
        this.page.current,
        this.page.size
      );
      console.log(data);
      this.list = data.records;
      this.page.total = data.total;
    },
    // 根据id删除文章
    handleDelete(id) {
      this.$confirm("此操作将永久删除该文章, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          articleApi.deleteById(id).then(res => {
            console.log(res, "删除成功");
          });
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          
        });
    }
  }
};
</script>